package com.bignerdranch.android.furnitureapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bignerdranch.android.furnitureapplication.BuyFurnitureActivity;
import com.bignerdranch.android.furnitureapplication.R;
import com.bignerdranch.android.furnitureapplication.entity.FurnitureInfoBean;
import com.bumptech.glide.Glide;

import java.util.List;

public class FurnitureInfoAdapter extends RecyclerView.Adapter<FurnitureInfoAdapter.ViewHolder> {

    private Context mContext;
    private List<FurnitureInfoBean> dataList;

    public class ViewHolder extends RecyclerView.ViewHolder {

        CardView furnitureItem;
        ImageView furnitureImage;
        TextView furnitureTitle;
        TextView furniturePrice;

        public ViewHolder(View itemView) {
            super(itemView);
            furnitureItem = itemView.findViewById(R.id.furniture_buy_item);
            furnitureImage = itemView.findViewById(R.id.furniture_info_image);
            furnitureTitle = itemView.findViewById(R.id.furniture_info_title);
            furniturePrice = itemView.findViewById(R.id.furniture_info_price);

        }
    }

    public FurnitureInfoAdapter(List<FurnitureInfoBean> dataList) {
        this.dataList = dataList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.furniture_item, parent, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.furnitureItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = viewHolder.getAdapterPosition();
                FurnitureInfoBean furnitureInfoBean = dataList.get(position);
                Intent intent = new Intent(mContext, BuyFurnitureActivity.class);
                intent.putExtra("furn_info", furnitureInfoBean);
                intent.putExtras(intent);
                mContext.startActivity(intent);
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        FurnitureInfoBean bean = dataList.get(position);
        holder.furniturePrice.setText("￥" + bean.getRedwood_price());
        holder.furnitureTitle.setText(bean.getRedwood_name());
        Glide.with(mContext).load(bean.getRedwood_image()).into(holder.furnitureImage);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }
}
